package org.jason.user.controller;


import cn.dev33.satoken.annotation.SaCheckBasic;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.jason.common.satoken.annotation.SaInterior;
import org.jason.model.common.dtos.ResponseResult;
import org.jason.model.common.enums.HttpCodeEnum;
import org.jason.model.user.dtos.ChangePasswordDto;
import org.jason.model.user.dtos.EditPersonalInfoDto;
import org.jason.model.user.dtos.LoginDto;
import org.jason.model.user.dtos.RegisterDto;
import org.jason.model.user.pojos.CuUser;
import org.jason.user.service.ICuUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 用户登录接口
 * </p>
 *
 * @author jason
 * @since 2023-07-19
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户登录模块", value = "UserController")
public class UserController {

    @Autowired
    private ICuUserService userService;

    @SaIgnore
    @ApiOperation("登录")
    @ApiResponses({@ApiResponse(code = 200, message = "登录成功"), @ApiResponse(code = 2, message = "用户名或密码错误"), @ApiResponse(code = 3, message = "请勿重复登录")})
    @PostMapping("/login")
    public ResponseResult login(@RequestBody @Validated LoginDto loginDto) {
        return userService.login(loginDto);
    }

    @ApiOperation("检查用户是否登录")
    @ApiResponse(code = 200, message = "返回用户token")
    @SaIgnore
    @GetMapping("/checkLogin")
    public ResponseResult checkLogin() {
        boolean isLogin = StpUtil.isLogin();
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        if (isLogin) {
            return new ResponseResult<>(HttpCodeEnum.SUCCESS, "用户已登录", tokenInfo);
        } else {
            return new ResponseResult<>(HttpCodeEnum.NEED_LOGIN, "用户未登录", false);
        }
    }

    @SaIgnore
    @ApiOperation("注册")
    @ApiResponses({@ApiResponse(code = 200, message = "注册成功"), @ApiResponse(code = 1000, message = "用户已存在"), @ApiResponse(code = 503, message = "注册失败"),})
    @PostMapping("/register")
    public ResponseResult register(@RequestBody @Validated RegisterDto registerDto) {
        return userService.register(registerDto);
    }

    @ApiOperation("注销登录")
    @ApiResponse(code = 200, message = "注销成功")
    @SaCheckLogin
    @GetMapping("/logout")
    public ResponseResult logout() {
        StpUtil.logout();
        return ResponseResult.get(HttpCodeEnum.SUCCESS, "注销成功！");
    }

    @ApiOperation("获取个人信息")
    @SaCheckLogin
    @GetMapping("/info")
    public ResponseResult<CuUser> getPersonalInfo() {
        return userService.getPersonalInfo();
    }

    @ApiOperation("获取个人信息")
    @SaInterior
    @GetMapping("/info/{userId:^[1-9]\\d*$}")
    public ResponseResult<CuUser> getUserInfo(@PathVariable Integer userId) {
        return userService.getUserInfo(userId);
    }

    @ApiOperation("修改个人信息")
    @SaCheckLogin
    @PutMapping("/info")
    public ResponseResult editPersonalInfo(@RequestBody @Validated EditPersonalInfoDto dto) {
        return userService.editPersonalInfo(dto);
    }

    @ApiOperation("修改密码")
    @SaCheckLogin
    @PostMapping("/changePassword")
    public ResponseResult changePassword(@RequestBody @Validated ChangePasswordDto dto) {
        return userService.changePassword(dto);
    }

    @ApiOperation("获取用户表")
    @SaInterior
    @GetMapping("/table")
    public List<CuUser> getUserTable() {
        return userService.list();
    }
}
